<?php
Class Categories extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('admin_id'));
        $this->data['controller'] = 'categories';
        $this->load->model('Mcategories');
    }
    
    public function index()
    {
        $this->data['id_active'] = $this->getParamUri(4,3);
        $this->load->library('pagination');
        $config['per_page'] = 12;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
		
        $data = $this->Mcategories->getCategoriesDB($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        //echo "<pre>"; die(print_r($this->data['data']));

        $this->data['action'] = 'index';
        $this->load->view('template/layout', $this->data);
    }
    
    public function add()
    {       
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', 'Tên danh mục', 'required');
        $this->form_validation->set_rules('active', 'Trạng thái', 'required');
        $this->form_validation->set_rules('img_incens_hd_', 'Hình ảnh', 'required');
       
        if($this->form_validation->run() === TRUE)
        {
            $name = $this->getParamString('name');
            $active = $this->getParamInt('active');
			
			$path1 = '';
			@mkdir(PROJECT_PATH.'/data/'.$path1, 0301);
			$config['upload_path'] = '../data/'.$path1;
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '2000';
			$config['max_width']  = '10240';
			$config['max_height']  = '7680';
			$config['encrypt_name'] =  TRUE;// TRUE/FALSE (boolean)
			$this->load->library('upload', $config);
			if( !$this->upload->do_upload('fileupload')){
				$image = '';
			}else{
				$data_img = $this->upload->data();
				$image = $path1.$data_img['file_name'];
			}
			//die(''.$image);
            $run = $this->Mcategories->addCategoriesDb($this->session->userdata('admin_id'),$name,$active,$image);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm thành công");window.location.href="'.$this->data['baseurl'].'manage/categories/index";</script>');
            }
        }
        $this->data['action'] = 'add';
        $this->load->view('template/layout', $this->data);
        
    }
    
	public function delete(){
        $id = $this->getParamUri(4,1);
        $run = $this->Mcategories->deleteCategoriesDB($id);
        if(intval($run) == 200)
        {
            die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>alert("Xóa thành công");window.location="'.$this->data['baseurl'].'manage/categories/index";</script>');
        }
    }

    public function edit()
    {
		$id = $this->getParamUri(4,1);
		$this->data['data'] = $this->Mcategories->getCategoriesDetail($id);
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', 'Tên danh mục', 'required');
        $this->form_validation->set_rules('active', 'Trạng thái', 'required');
        //$this->form_validation->set_rules('img_incens_hd_', 'Hình ảnh', 'required');
        if($this->form_validation->run() === TRUE)
        {
			$name = $this->getParamString('name');
            $active = $this->getParamInt('active');
            $dataimage_check = $this->getParamString('img_check');
            $verify_image = $this->getParamString('img_incens_verify');
            if(!in_array($verify_image, array(null, '')))
            {
                if($verify_image != $dataimage_check)
                {
                    @unlink(PROJECT_PATH.'/data/'.$verify_image);
                }
            }
            $path1 = '';
			@mkdir(PROJECT_PATH.'/data/'.$path1, 0301);
			$config['upload_path'] = '../data/'.$path1;
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '2000';
			$config['max_width']  = '10240';
			$config['max_height']  = '7680';
			$config['encrypt_name'] =  TRUE;// TRUE/FALSE (boolean)
			$this->load->library('upload', $config);
			if( !$this->upload->do_upload('fileupload')){
				$image = $dataimage_check;
			}else{
				$data_img = $this->upload->data();
				$image = $path1.$data_img['file_name'];
			}
			
			
            $run = $this->Mcategories->editCategoriesDb($this->session->userdata('admin_id'),$name,$active,$image,$id);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/categories/index";</script>');
            }
        }
        $this->data['action'] = 'edit';
        $this->load->view('template/layout', $this->data);
    }
    
    
    
}